/**
 * futures : masterlink.futures.dao.AccountDao.java
 */
package masterlink.futures.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import masterlink.futures.vo.Account;
import masterlink.futures.vo.Futcan;
import masterlink.futures.vo.FutcanGroup;
import masterlink.futures.vo.Group;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/10/25
 * @version 1.0.0	
 */
@Repository
@Qualifier("FutcanDao")
public class FutcanDao extends TimesTenTemplateDao {
	private static final String FIND_MONTH="select setMonth from futcan "+
											"group by setMonth order by setMonth ";	
	
	private static final String FIND_BY_GROUP="select c.trid, c.setmonth, c.strikeprice,"+
												" (sum(c.coverqty)/2) as coverqty, sum(c.coverincome) as coverincome,"+
												" sum(c.fee) as fee, sum(c.tax) as tax"+
												" from greeks_limit a, fuminv b, futcan c"+
												" where a.account=b.acno and a.broker=b.broker_id"+
												" and b.broker_id=c.broker_id and b.acno=c.acno"+
												" and a.id=?"+
												" group by c.trid, c.setmonth, c.strikeprice"+
												" order by c.trid, c.setmonth, c.strikeprice";
	
	private static final String FIND_BY_TRID="select c.*, b.name"+
												" from greeks_limit a, fuminv b, futcan c"+
												" where a.account=b.acno and a.broker=b.broker_id "+
												" and b.broker_id=c.broker_id and b.acno=c.acno"+
												" and a.id=?"+
												" and c.trid=? and c.setmonth=? and c.strikeprice=?"+
												" order by c.broker_id, c.acno";
	
	private static final String FIND_BY_ACCOUNT="select c.*, b.name"+
												" from greeks_limit a, fuminv b, futcan c"+
												" where a.account=b.acno and a.broker=b.broker_id "+
												" and b.broker_id=c.broker_id and b.acno=c.acno"+
												" and a.id=?"+
												" and a.account=?"+
												" order by c.COVERSEQNO asc, c.buysell";
	
	private static final String FIND_BY_ACCOUNT_MONTH="select c.*, b.name"+
			" from greeks_limit a, fuminv b, futcan c"+
			" where a.account=b.acno and a.broker=b.broker_id "+
			" and b.broker_id=c.broker_id and b.acno=c.acno"+
			" and a.id=?"+
			" and a.account=?"+
			" and c.setmonth=?"+
			" order by c.COVERSEQNO asc, c.buysell";
	@SuppressWarnings("unchecked")
	public List<BigDecimal> findMonth(){
		return (List<BigDecimal>) super.findBySql(FIND_MONTH);
	}
	
	public List<FutcanGroup> findByGroup(Group g){
		if(g==null){
			log.warn("The given Group is null for findByGroup(Group g).");
			return null;
		}
		return findBySql(FIND_BY_GROUP, FutcanGroup.class, g.getId());
	}
	
	public List<Futcan> findByTrid(Group g,String trid,String setmonth,String strikeprice){
		if(g==null){
			log.warn("The given Group is null for findByGroup(Group g).");
			return null;
		}
		return findBySql(FIND_BY_TRID, Futcan.class, g.getId(),trid,setmonth,strikeprice);
	}
	
	public List<Futcan> findByAcc(Group g,Account acc){
		if(g==null){
			log.warn("The given Group is null for findByGroup(Group g).");
			return null;
		}
		return findBySql(FIND_BY_ACCOUNT, Futcan.class, g.getId(),acc.getId().getAcno());
	}
	public List<Futcan> findByAcc(Group g,Account acc,String month){
		if(g==null){
			log.warn("The given Group is null for findByGroup(Group g).");
			return null;
		}
		return findBySql(FIND_BY_ACCOUNT_MONTH, Futcan.class, g.getId(),acc.getId().getAcno(),month);
	}
}
